﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using KGE.Define;

namespace KGE.Tools
{
    public partial class CharacterActionMaker : Form
    {
        Character.Action act;
        List<KImagePackage> kips;

        public CharacterActionMaker()
        {
            InitializeComponent();
            timer1.Enabled = true; ;
        }

        private void gui_gen_Click(object sender, EventArgs e)
        {
            int fo = int.Parse(gui_frmf.Text);
            int to = 0;
            try
            {
                to = int.Parse(gui_frmt.Text);
            }
            catch
            {
                to = fo;
            }


            for (int i = fo; i <= to; i++)
            {
                Character.Action.Frame frame = new Character.Action.Frame();
                frame.KImageIndexer = new ObjectIndexer(null, i);
                frame.Length = 0.1;
                frame.FilterColor = Color.White;
                act.Frames.Add(frame);
            }
        }

        void update_gui()
        {
            label18.Text = (trackBar1.Value + 1) + "/" + (trackBar1.Maximum + 1);

            if (act == null)
            {
                return;
            }
            if (act.Frames.Count == 0)
            {
                return;
            }

            Character.Action.Frame frame = act.Frames[trackBar1.Value];
            try
            {
            gui_frmlen.Text = "" + frame.Length;
            gui_frmx.Text = "" + frame.DrawRectangle.X;
            gui_frmy.Text = "" + frame.DrawRectangle.Y;
            gui_frmw.Text = "" + frame.DrawRectangle.Width;
            gui_frmh.Text = "" + frame.DrawRectangle.Height;
            gui_frmimg.Text = "" + frame.KImageIndexer.Index;
                gui_frmsound.Text = "" + frame.KSoundIndexer.Name;
            }
            catch { }

            trackBar1.Maximum = act.Frames.Count - 1;
            gui_frmloop.Checked = act.IsLoop;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            update_gui();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            act = new Character.Action(gui_name.Text);
            //act.DrawZone.X = int.Parse(gui_opointx.Text);
            //act.DrawZone.Y = int.Parse(gui_opointy.Text);
            //act.DrawZone.Width = int.Parse(gui_draww.Text);
            //act.DrawZone.Height = int.Parse(gui_drawh.Text);
            kips = new List<KImagePackage>();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "Kevin Character Action|*.KCA";
            if (fd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (fd.FileName == string.Empty)
            {
                return;
            }
            open(fd.FileName);
        }

        public void open(string fn)
        {
            act = (Character.Action)Util.LoadObject(fn);
            kips = new List<KImagePackage>();
        }

        private void gui_frmok_Click(object sender, EventArgs e)
        {
            if (act == null)
            {
                return;
            }
            if (act.Frames.Count == 0)
            {
                return;
            }

            Character.Action.Frame frame = act.Frames[trackBar1.Value];
            frame.Length = double.Parse(gui_frmlen.Text);
            frame.DrawRectangle.X = int.Parse(gui_frmx.Text);
            frame.DrawRectangle.Y = int.Parse(gui_frmy.Text);
            frame.DrawRectangle.Width = int.Parse(gui_frmw.Text);
            frame.DrawRectangle.Height = int.Parse(gui_frmh.Text);
            frame.KImageIndexer = new ObjectIndexer(null, int.Parse(gui_frmimg.Text));
            frame.KSoundIndexer = new ObjectIndexer(gui_frmsound.Text);
            act.IsLoop = gui_frmloop.Checked;
        }

        private void panel1_Enter(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void panel1_Leave(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void gui_kipadd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.RestoreDirectory = true; 
            ofd.Filter = "Kevin Image Package|*.kip";
            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (ofd.FileName == string.Empty)
            {
                return;
            }
            KImagePackage kip = (KImagePackage)Util.LoadObject(ofd.FileName);
            kips.Add(kip);
            gui_avatar.Items.Add(kip.Name);
            pictureBox1.Refresh();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (kips == null)
            {
                return;
            }
            if (kips.Count == 0)
            {
                return;
            }
            if (act == null)
            {
                return;
            } 
            if (act.Frames.Count == 0)
            {
                return;
            }
            Character.Action.Frame frame = act.Frames[trackBar1.Value];

            Graphics g = e.Graphics;
            g.Clear(Color.Gray);

            for (int i = 0; i < kips.Count; i++)
            {
                //string name = (string)gui_avatar.Items[i];
                Image img = Image.FromStream(kips[i][frame.KImageIndexer.Index].DataStream);
                Point p = kips[i][frame.KImageIndexer.Index].DrawRectangle.Location;
                g.DrawImage(img, p);
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

        private void gui_clr_Click(object sender, EventArgs e)
        {
            act.Frames.Clear();
        }

        private void gui_play_Click(object sender, EventArgs e)
        {
            if (act.Frames.Count == 0)
            {
                return;
            }
            if (trackBar1.Value >= trackBar1.Maximum)
            {
                trackBar1.Value = 0;
            }
            int i = trackBar1.Value;
            Character.Action.Frame frame = act.Frames[i];
            timer2.Interval = (int)(frame.Length * 1000.0);
            timer2.Enabled = !timer2.Enabled;
            pictureBox1.Refresh();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            if (trackBar1.Value < trackBar1.Maximum)
            {
                trackBar1.Value++;
            }
            else
            {
                if (!gui_isloop.Checked)
                {
                    timer2.Enabled = false;
                    return;
                }
                else
                {
                    trackBar1.Value = 0;
                }
            }
            int i = trackBar1.Value;
            Character.Action.Frame frame = act.Frames[i];
            timer2.Interval = (int)(frame.Length * 1000.0);
            pictureBox1.Refresh();
            timer2.Enabled = true;
        }

        private void gui_kipdel_Click(object sender, EventArgs e)
        {
            int i = gui_avatar.SelectedIndex;
            if (i < 0)
            {
                return;
            }
            gui_avatar.Items.RemoveAt(i);
            kips.RemoveAt(i);
        }

        private void gui_yesall_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < act.Frames.Count; i++)
            {
                act.Frames[i].Length = double.Parse(gui_frmlen.Text);
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save();
        }

        void save()
        {
            if (act == null)
            {
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "Kevin Character Action|*.KCA";
            if (sfd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Util.SaveObject(sfd.FileName, act);
            //Util.SaveObjectXML(sfd.FileName + ".XML", act);
        }

        private void gui_kip_up_Click(object sender, EventArgs e)
        {
            int i = gui_avatar.SelectedIndex;
            if (i - 1 < 0)
            {
                return;
            }

            KImagePackage kip = new KImagePackage();
            kip = kips[i];

            gui_avatar.Items.RemoveAt(i);
            kips.RemoveAt(i);

            gui_avatar.Items.Insert(i - 1, kip.Name);
            kips.Insert(i - 1, kip);
        }

        private void gui_frmdel_Click(object sender, EventArgs e)
        {
            int i = trackBar1.Value;
            //if (i - 1 < 0)
            //{
            //    return;
            //}

            act.Frames.RemoveAt(i);
            if(trackBar1.Value>0)
            trackBar1.Maximum--;
        }
    }
}
